package com.ai.opc;

import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;

import java.util.concurrent.CompletableFuture;

/**
 * @Author jiaok
 * @Date 2024-11-08 11:10
 */
public class OpcClientDemo2 {

    public static void main(String[] args) throws Exception {
        // 创建一个OPC UA客户端
        OpcUaClient client = OpcUaClient.create("opc.tcp://DESKTOP-2JE3EHU:53530/OPCUA/SimulationServer");

        // 连接到OPC UA服务端
        client.connect().get();

        // 读取Objects节点下的某个变量
        NodeId nodeId = new NodeId(3, "1008");
//        CompletableFuture<Variant> cf = client.readValue(0, UInteger.valueOf(0), nodeId);
        CompletableFuture<DataValue> cf = client.readValue(0, TimestampsToReturn.Neither, nodeId);
        // 获取读取结果
//        Variant value = cf.get();
        DataValue value = cf.get();  // 阻塞直到获取结果

        System.out.println("Value: " + value.getValue());

        // 断开连接
        client.disconnect().get();
    }

}
